﻿#include <QtMath>

#include "mapconvert.h"

QString Custom::latToString(double lat)
{
    double absLat = qAbs(lat);
    if (absLat > 90)//非法数值
    {
        return QString();
    }

    QString result;
    if (lat == 0)
    {
        return QStringLiteral("纬度00°00.000′");
    }
    else if (lat > 0)
    {
        result += QStringLiteral("北纬");
    }
    else
    {
        result += QStringLiteral("南纬");
    }

    result += doubleToAngle(absLat, 2);
    return result;
}

QString Custom::lonToString(double lon)
{
    double absLon = qAbs(lon);
    if (absLon > 180)//非法数值
    {
        return QString();
    }

    QString result;
    if (lon == 0)
    {
        return QStringLiteral("经度000°00.000′");
    }
    else if (absLon == 180)
    {
        return QStringLiteral("经度180°00.000′");
    }
    else if (lon > 0)
    {
        result += QStringLiteral("东经");
    }
    else
    {
        result += QStringLiteral("西经");
    }

    result += doubleToAngle(absLon, 3);
    return result;
}

QString Custom::doubleToAngle(double d, int degreeBit)
{
    static const int sc_angleInterval = 60;
    static const int sc_minutePrecision = 3;//分的精度
    int degree = qFloor(d);//度
    double remainder = (d - degree) * sc_angleInterval;
    int remainderInt = qFloor(remainder);
    int remainderDec = qFloor((remainder - remainderInt) * qPow(10, sc_minutePrecision));
    QString result = QStringLiteral("%1°%2.%3′");
    result = result.arg(degree, degreeBit, 10, QChar('0'))
             .arg(remainderInt, 2, 10, QChar('0'))
             .arg(remainderDec, sc_minutePrecision, 10, QChar('0'));
    return result;
}

QString Custom::range(double east, double west, double north, double sourth)
{
    QString range;
    range += QStringLiteral("从") + latToString(sourth);//纬度
    range += QStringLiteral("到") + latToString(north) + "\n";
    range += QStringLiteral("从") + lonToString(west);//经度
    range += QStringLiteral("到") + lonToString(east);
    return range;
}
